package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV1;
import com.education.realtime.bean.TestExamCourse;
import com.education.realtime.common.Constant;
import com.education.realtime.fuction.DimAsyncFunction;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class Dws_Exam_Course extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_Exam_Course().init(3001,"Dws_Exam_Course", Constant.TOPIC_DWD_EXAM);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //将流转换成javabean
        SingleOutputStreamOperator<TestExamCourse> examCourse = parseToBean(stream);
        //补充维度信息
        SingleOutputStreamOperator<TestExamCourse> dimstream = joinDim(examCourse);
        //聚合
        reducedim(dimstream);


    }

    private void reducedim(SingleOutputStreamOperator<TestExamCourse> dimstream) {
        dimstream.assignTimestampsAndWatermarks(WatermarkStrategy.<TestExamCourse>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                .withTimestampAssigner((obj,ts)->obj.getTs()))
                .keyBy(TestExamCourse::getCourse_id)
                .process(new KeyedProcessFunction<String, TestExamCourse, TestExamCourse>() {
                    @Override
                    public void processElement(TestExamCourse testExamCourse, Context context,
                                               Collector<TestExamCourse> collector) throws Exception {
                        Long user_num = 1L;
                        testExamCourse.setUser_num(user_num);
                        collector.collect(testExamCourse);
                    }
                })
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TestExamCourse>() {
                    @Override
                    public TestExamCourse reduce(TestExamCourse t2, TestExamCourse t1) throws Exception {
                        t1.setUser_num(t1.getUser_num() + t2.getUser_num());
                        t1.setDuration_ts(t1.getDuration_ts() + t2.getDuration_ts());
                        t1.setScore_num(t1.getScore_num() + t2.getScore_num());
                        return t1;
                    }
                }, new ProcessAllWindowFunction<TestExamCourse, TestExamCourse, TimeWindow>() {
                    @Override
                    public void process(Context context, Iterable<TestExamCourse> iterable, Collector<TestExamCourse> collector) throws Exception {
                        TestExamCourse bean = iterable.iterator().next();
                        String stt = AtguiguUtil.toDateTime(context.window().getStart());
                        String edt = AtguiguUtil.toDateTime(context.window().getEnd());
                        bean.setStt(stt);
                        bean.setEdt(edt);
                        collector.collect(bean);
                    }
                })
                .addSink(FlinkSinkUtil.getClickHouseSink(Constant.CLICKHOUSE_DB,"dws_exam_course_window",TestExamCourse.class));
    }

    private SingleOutputStreamOperator<TestExamCourse> joinDim(SingleOutputStreamOperator<TestExamCourse> examCourse) {
      return AsyncDataStream.unorderedWait(
               examCourse,
                new DimAsyncFunction<TestExamCourse>(){

                    @Override
                    public void addDim(TestExamCourse input, JSONObject dim) {
                        input.setCourse_id(dim.getString("course_id"));
                    }

                    @Override
                    public String getTableName() {
                        return "dim_test_paper";
                    }

                    @Override
                    public String getDimId(TestExamCourse input) {
                        return input.getCourse_id();
                    }
                },
                120,
                TimeUnit.SECONDS
        );
    }

    private SingleOutputStreamOperator<TestExamCourse> parseToBean(DataStreamSource<String> stream) {
      return  stream.map(JSON::parseObject).map(obj-> TestExamCourse.builder().score_num(obj.getDouble("score"))
                .duration_ts(obj.getLong("duration_sec")).user_num(1L).ts(obj.getLong("ts")*1000).build());

    }
}
